/*
 * SoftwareSAR - 1.0
 */
package br.com.cpqd.ssar.mediador.gui;

import br.com.cpqd.ssar.mediador.ControladorFonteEletromagnetica;
import br.com.cpqd.ssar.utilitarias.FonteEletromagnetica;
import br.com.cpqd.ssar.utilitarias.FonteEletromagneticaSimulacao;
import br.com.cpqd.ssar.utilitarias.ListaFontesEletromagneticas;
import java.awt.Color;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Janela de Edição de um Material
 * @author  Carlos H. S. Santos
 * @version 1.1
 */
public class JanelaCriarFonteEletromagneticaSimulacao extends javax.swing.JDialog {
    /*
     * Coordenadas da Fonte Inserida na Cena de Simulacao
     */
    private Vector coordenadas;
    
    /*
     * Fonte Eletromagnetica do Usuario
     */
    FonteEletromagnetica fonteEletromagnetica;
    /*
     * Fonte Eletromagnetica da Simulação
     */
    FonteEletromagneticaSimulacao fonteEletromagneticaSimulacao;
    
    
    
    /*
     * Lista de Fonte Eletromagnetica da Simulação
     */
    ListaFontesEletromagneticas listaFontes;
    
    /*
     * Reconhece o último item inserido na Lista Fontes de Simulacao
     */
    private int lastId;
    
    
    /**
     * Retornar a Fonte Eletromagnética Cadastrada para a Simulação
     * @return fonte FonteEletromagneticaSimulacao
     */
    public FonteEletromagneticaSimulacao getFonteSimulacao(){
        return fonteEletromagneticaSimulacao;
    }
    

    /**
     * Método Construtor
     * @param controladorJanelas ControladorJanelas
     * @param parent Frame
     * @param modal determina se a janela é modal ou não (impede o acesso as janelas abaixo)
     */
    public JanelaCriarFonteEletromagneticaSimulacao(java.awt.Frame parent, 
            boolean modal, Vector coordenadas, int s){
        super(parent, modal);
        initComponents();        
        //controlarFonte = new ControladorFonteEletromagnetica();
        fonteEletromagneticaSimulacao = null;
        
        //this.controlarFonte           = controladorFonte;        
        try {
            this.listaFontes = new ListaFontesEletromagneticas();
        } catch (IOException ex) {
            Logger.getLogger(JanelaCriarFonteEletromagneticaSimulacao.class.getName()).log(Level.SEVERE, null, ex);
        }
        //this.listaFontes = listaFontes;
       
        this.lastId = s;
        this.coordenadas = new Vector();
        this.coordenadas = coordenadas;
        jtxtX.setText(coordenadas.get(0).toString());
        jtxtY.setText(coordenadas.get(1).toString());
        jtxtZ.setText(coordenadas.get(2).toString()); 
        jtxtNome.setText("Fonte["+s+"]");
        //jComboBoxListaFontes = new JComboBox();
        //jComboBoxListaFontes.setModel(listaFontes.getModelo());
        for(int cont=0; cont <listaFontes.size(); cont++){
            jComboBoxListaFontes.insertItemAt(listaFontes.getFonte(cont), cont);
        }
        jComboBoxListaFontes.setSelectedIndex(0);
        setVisible(true);

    }

    
        /**
     * Tenta Aplicar as Propriedades Editadas
     */
    private FonteEletromagneticaSimulacao tentarAplicarPropriedades() {
        boolean nenhumErro  = true;
        // Testar P1

        try {
            if (Float.valueOf(jtxtX.getText())>0)
                jtxtX.setForeground(Color.BLUE);
        } catch (Exception e) {
            jtxtX.setForeground(Color.BLACK);
            nenhumErro = false;
        }
    
        try {
            if (Float.valueOf(jtxtY.getText())>0)
                jtxtY.setForeground(Color.BLACK);
        } catch (Exception e) {
            jtxtY.setForeground(Color.RED);
            nenhumErro = false;
        }

        try {
            if (Float.valueOf(jtxtZ.getText())>0)
                jtxtZ.setForeground(Color.BLACK);
        } catch (Exception e) {
            jtxtZ.setForeground(Color.RED);
            nenhumErro = false;
        }
        
        try {
            if (jtxtNome==null)
                jtxtNome.setForeground(Color.BLACK);
        } catch (Exception e) {
            jtxtNome.setForeground(Color.RED);
            nenhumErro = false;
        }
        
        if (nenhumErro==true){
            this.fonteEletromagnetica = new FonteEletromagnetica();
            this.fonteEletromagnetica = (FonteEletromagnetica) jComboBoxListaFontes.getSelectedItem();            
            this.fonteEletromagneticaSimulacao = new FonteEletromagneticaSimulacao();
            this.fonteEletromagneticaSimulacao.setIdType(fonteEletromagnetica.getIdFonte());
            this.fonteEletromagneticaSimulacao.setNome(jtxtNome.getText());
            this.fonteEletromagneticaSimulacao.setX(Float.valueOf(jtxtX.getText()));
            this.fonteEletromagneticaSimulacao.setY(Float.valueOf(jtxtY.getText()));
            this.fonteEletromagneticaSimulacao.setZ(Float.valueOf(jtxtZ.getText()));
            this.fonteEletromagneticaSimulacao.setCor(jPanelCor.getBackground());
            return fonteEletromagneticaSimulacao;
        }
        else {
            return null;
        }
            
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBoxListaFontes = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jtxtX = new javax.swing.JTextField();
        jtxtY = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtxtZ = new javax.swing.JTextField();
        jPanelCor = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jtxtNome = new javax.swing.JTextField();
        btnConfirmar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Inserir Fonte Eletromagnética Simulação");
        setLocationByPlatform(true);
        setModal(true);
        setResizable(false);

        jPanel3.setBackground(new java.awt.Color(231, 240, 250));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Inserir Fonte Eletromagnética Simulação"));

        jLabel1.setText("Fonte:");

        jComboBoxListaFontes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxListaFontesActionPerformed(evt);
            }
        });

        jLabel2.setText("x:");

        jLabel3.setText("y:");

        jLabel4.setText("z:");

        jtxtZ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtZActionPerformed(evt);
            }
        });

        jPanelCor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout jPanelCorLayout = new javax.swing.GroupLayout(jPanelCor);
        jPanelCor.setLayout(jPanelCorLayout);
        jPanelCorLayout.setHorizontalGroup(
            jPanelCorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 43, Short.MAX_VALUE)
        );
        jPanelCorLayout.setVerticalGroup(
            jPanelCorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 18, Short.MAX_VALUE)
        );

        jLabel5.setText("Nome:");

        btnConfirmar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/cpqd/ssar/gerenciador/gui/recursos/confirmar.png"))); // NOI18N
        btnConfirmar.setText("Confirmar");
        btnConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/cpqd/ssar/gerenciador/gui/recursos/Cancelar.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jComboBoxListaFontes, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(jPanelCor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(108, Short.MAX_VALUE)
                .addComponent(btnConfirmar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancelar)
                .addContainerGap())
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 318, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3)
                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtxtZ)
                    .addComponent(jtxtY)
                    .addComponent(jtxtX, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanelCor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jComboBoxListaFontes, javax.swing.GroupLayout.DEFAULT_SIZE, 18, Short.MAX_VALUE))))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jtxtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(21, 21, 21)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jtxtY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtxtZ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnConfirmar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jComboBoxListaFontesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxListaFontesActionPerformed
// TODO add your handling code here:
    FonteEletromagnetica fonteEletromagneticaSimulacao = new FonteEletromagnetica();
    fonteEletromagneticaSimulacao = (FonteEletromagnetica) jComboBoxListaFontes.getSelectedItem();
    jPanelCor.setBackground(fonteEletromagneticaSimulacao.getColor());
    
}//GEN-LAST:event_jComboBoxListaFontesActionPerformed

private void jtxtZActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtZActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jtxtZActionPerformed



private void btnConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmarActionPerformed
            if ((fonteEletromagneticaSimulacao= this.tentarAplicarPropriedades()) != null) {
                
                dispose();
            }
}//GEN-LAST:event_btnConfirmarActionPerformed

private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
// TODO add your handling code here:        
    
    dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConfirmar;
    private javax.swing.JComboBox jComboBoxListaFontes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanelCor;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField jtxtNome;
    private javax.swing.JTextField jtxtX;
    private javax.swing.JTextField jtxtY;
    private javax.swing.JTextField jtxtZ;
    // End of variables declaration//GEN-END:variables
}
